package com.example.service.impl;

import com.example.common.exception.ConcurrentException;
import com.example.common.exception.ParamException;
import com.example.common.response.ServerResponse;
import com.example.dto.DemoDTO;
import com.example.entity.Demo;
import com.example.mapper.DemoMapper;
import com.example.service.DemoService;
import com.example.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: demo业务处理实现类
 * @Auther: Swain
 * @Date: 2021/1/11 16:48
 * @Version 1.0.0
 */
@Log4j2
@Service
public class DemoServiceImpl implements DemoService {


    @Autowired
    private DemoMapper demoMapper;

    @Override
    public ServerResponse addSingle(DemoDTO demoDTO) {
        Demo demo = new Demo();
        BeanUtils.copyProperties(demoDTO, demo);
        demo.setId(null);
        demo.setDemoId(IdUtils.getRandomId());
        demo.setVersion(0L);
        int insert = demoMapper.insert(demo);
        return ServerResponse.createBySuccess(demo.getDemoId());
    }

    @Override
    public ServerResponse deleteSingle(DemoDTO demoDTO) {
        DemoDTO dto = demoMapper.selectByDemoId(demoDTO.getDemoId());
        if (ObjectUtils.isEmpty(dto)) {
            log.error("demoId异常");
            throw new ParamException("demoId异常");
        }
        int delete = demoMapper.deleteByPrimaryKey(dto.getId());
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse updateSingle(DemoDTO demoDTO) {
        DemoDTO dto = demoMapper.selectByDemoId(demoDTO.getDemoId());
        if (ObjectUtils.isEmpty(dto)) {
            log.error("demoId异常");
            throw new ParamException("demoId异常");
        }
        if(!dto.getVersion().equals(demoDTO.getVersion())){
            log.error("数据过时");
            throw new ConcurrentException();
        }
        demoDTO.setId(dto.getId());
        Demo demo = new Demo();
        BeanUtils.copyProperties(demoDTO, demo);
        int update = demoMapper.updateByPrimaryKeySelective(demo);
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse findSingle(DemoDTO demoDTO) {
        DemoDTO dto = demoMapper.selectByDemoId(demoDTO.getDemoId());
        if (ObjectUtils.isEmpty(dto)) {
            return ServerResponse.createByNoData();
        }
        return ServerResponse.createBySuccess(dto);
    }

    @Override
    public ServerResponse findListPage(DemoDTO demoDTO) {
        PageHelper.startPage(demoDTO.getPageNum(), demoDTO.getPageSize());
        List<DemoDTO> dtoList = demoMapper.findListByParams(demoDTO);
        if (ObjectUtils.isEmpty(dtoList)) {
            return ServerResponse.createByNoData();
        }
        PageInfo pageInfo = new PageInfo(dtoList);
        return ServerResponse.createBySuccess(pageInfo);
    }
}
